package com.cl.mypureframe.activity;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cl.mypureframe.R;
import com.cl.mypureframe.model.MsgMod;
import com.cl.mypureframe.util.GLobalConstant;
import com.cl.mypureframe.util.ImageUtils;
import com.cl.mypureframe.util.Log;
import com.cl.mypureframe.util.UtilFile;

import org.jetbrains.annotations.NotNull;

/**
 * @Class: MainActivity
 * @Description:
 * @Author: ChenLei
 * @Date: 2020/11/17---15:38
 */
public class MainActivity extends BasePicActivity {
    private MsgMod picMod;
    private BaseQuickAdapter<MsgMod, BaseViewHolder> myAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenViewAndBack(R.layout.activity_main, R.string.app_name);
        setTitleName(TAG);

        RecyclerView rv_main = findViewById(R.id.rv_main);
        myAdapter = new BaseQuickAdapter<MsgMod, BaseViewHolder>(R.layout.item_bcyk) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, MsgMod msgMod) {
                baseViewHolder.setText(R.id.tv_title, msgMod.getTitle());
                baseViewHolder.setText(R.id.tv_desc, msgMod.getMsg());

                ImageView iv = baseViewHolder.getView(R.id.iv_pic);
                if (msgMod.getType() == 1) {
                    ImageUtils.glideLoaderCropCircle(getContext(), msgMod.getPicurl(), iv);
                } else {
                    ImageUtils.glideLoaderRoundedCorners(getContext(), msgMod.getPicurl(), iv);
                }

            }
        };
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv_main.setLayoutManager(linearLayoutManager);
        rv_main.setAdapter(myAdapter);
        myAdapter.setNewInstance(MsgMod.functionalMoudule());
        myAdapter.setOnItemClickListener((adapter, view, position) -> {
            switch (position) {
                case 0:// 选择图片
                    picMod = (MsgMod) adapter.getItem(position);
                    showPop();
                    break;
                case 1:// 展示空布局
                    showEmptyView(true);
                    break;
                case 2:// 登录
                    startVerifyActivity(LoginActivity.class);
                    break;
                case 3:// 刷新列表
                    startVerifyActivity(MyListActivity.class);
                    break;
                case 4:// 图片选择与展示
                    startVerifyActivity(PictureSelectorActivity.class);
                    break;
                case 5:// 下载文件
                    startVerifyActivity(DownloadActivity.class);
                    break;
            }
        });

        // app初始化方法，主要通过动态权限,加载本地各种缓存文件夹
        applyPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    protected void getFilePath(String filePath) {
        picMod.setPicurl(filePath);
        myAdapter.notifyItemChanged(0);
    }

    @Override
    protected void onPermissionSuccess() {
        // 创建本app各种缓存文件夹
        Log.i(TAG, "initAppRoot--->onSuccess");
        UtilFile.createPathFile(GLobalConstant.PHOTO_PATH);
    }

    @Override
    public void onButtonClick(View view) {
        showEmptyView(false);
    }
}
